//
//  ZCHealthyVC.m
//  zctx
//
//  Created by apple on 16/1/4.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCHealthyVC.h"
#import "ZCHealthyCell.h"
#import "DZWebViewController.h"
#import "ZChealthyModelContent.h"
@interface ZCHealthyVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;

@property BOOL isnothing;
@end

@implementation ZCHealthyVC
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCHealthyVC"];
    [MTA trackPageViewBegin:@"ZCHealthyVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCHealthyVC"];
    [MTA trackPageViewEnd:@"ZCHealthyVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"健康豆明细";
    [self initnavegation];
    self.isnothing = NO;
    self.dataArray = [[NSMutableArray alloc]init];
    
    [self initTableView]; //初始化UITableView
    [self requestData];
    //    [self initTableViewHeader]; //创建TableviewHeader
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCHealthyCell" bundle:nil] forCellReuseIdentifier:@"healthycell"];
}

//请求网络数据
-(void)requestData{
    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    [NetworkManager requestMemberGetbeansdetailWithMid:UserModel.mid password:UserModel.password success:^(NSDictionary *data) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([[data objectForKey:@"content"] isKindOfClass:[NSArray class]]) {
            NSArray *array = (NSArray *)[data objectForKey:@"content"];
            for (NSDictionary *dict in array) {
                ZChealthyModelContent *model = [ZChealthyModelContent modelObjectWithDictionary:dict];
                [self.dataArray addObject:model];
            }
            
            if (self.dataArray.count>0) {
                self.isnothing = NO;
            }else{
                self.isnothing  = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView reloadData];

    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
            self.bannerView.hidden = NO;
        }
    }];
}


#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
    
    UIButton * rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 25)];
    [rightButton setTitle:@"健康豆规则" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton addTarget:self action:@selector(HealthyClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = barButtonItem_rightNavBar;
}

-(void)backButtonPressed{

    [self.navigationController popViewControllerAnimated:YES];
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestData];
}

-(void)HealthyClick{
    DZWebViewController *vc = [[DZWebViewController alloc] init];
    vc.strURL = @"http://www.zhongcetianxia.com/index.php/M/member/bean_rule";
    vc.title = @"健康豆规则";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorColor = RGB(236, 237, 238);
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"healthycell";
    ZCHealthyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[ZCHealthyCell alloc]initWithStyle:
                UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.healthyModel = self.dataArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.000001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 10)];
    return headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (self.isnothing==NO) {
        UIView *footView = [[UIView alloc]init];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        return footView;
    }else{
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,400)];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        
        UIImageView*redImg = [[UIImageView alloc]init];
        [footView addSubview:redImg];
        [redImg makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(footView.top).with.offset(0.255 * SCREEN_HEIGHT);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(100);
            make.width.mas_equalTo(100);
        }];
        [redImg setImage:[UIImage imageNamed:@"me_healthy_no"]];
        
        UILabel*redLable = [[UILabel alloc]init];
        [footView addSubview:redLable];
        [redLable makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(redImg.bottom).with.offset(23);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(21);
            make.width.mas_equalTo(150);
        }];
        redLable.textAlignment = NSTextAlignmentCenter;
        redLable.text = @"无健康豆";
        redLable.textColor = [UIColor colorWithHexString:@"#d2d2d2"];
        redLable.font = [UIFont systemFontOfSize:16];
        return footView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.isnothing==NO) {
        return 0.001;
    }else{
        return 400;
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
